<!--
  Copyright 2016 The LUCI Authors. All rights reserved.
  Use of this source code is governed under the Apache License, Version 2.0
  that can be found in the LICENSE file.
-->

<link rel="import" href="../inc/logdog-app-base/logdog-app-base.html">

<script>
  window.addEventListener('WebComponentsReady', function() {
    // We use Page.js for routing. This is a Micro
    // client-side router inspired by the Express router
    // More info: https://visionmedia.github.io/page.js/
    // Removes end / from app.baseUrl which page.base requires for production
    var baseUrl = app.baseUrl;
    page.base(baseUrl);

    // Middleware
    function scrollToTop(ctx, next) {
      app.scrollPageToTop();
      next();
    }
    function closeDrawer(ctx, next) {
      app.closeDrawer();
      next();
    }

    // Hack to make this work on root-URL apps (e.g., localhost testing).
    if ( baseUrl === "/" ) {
      var oldReplace = page.replace;
      page.replace = function(path, state, init, dispatch) {
        if ( path.substring(0, 1) !== "/" ) {
          path = "/" + path;
        }
        if ( path.substring(0, 3) !== "/#!" ) {
          path = "/#!" + path;
        }
        return oldReplace(path, state, init, dispatch);
      };
    }

    // Routes
    page("*", scrollToTop, closeDrawer, function(ctx, next) {
      next();
    });

    page("/", function() {
      app.route = "root";
    });

    page("/query/*", function(data) {
      app.route = "query";
      app.$.query.base = logdog.correctStreamPath(data.params[0]);
    });

    page("/stream/*", function(data) {
      app.route = "stream";
      app.$.stream.streams = logdog.getQueryValues(data.querystring, "s").
          map(logdog.correctStreamPath);
    });

    // 404
    page(function() {
      app.$.toast.text = "Can't find: " + window.location.href +
                         ". Redirected you to Home Page";
      app.$.toast.show();
      page.redirect(app.baseUrl);
    });

    page({
      hashbang: true,
    });
  });
</script>
